package com.adee.java.jdbc.util;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class StringUtils {

    public static void main(String[] args) {
        // 随机字符串
        System.out.println(getRandomString(10));
        // 中文名
        System.out.println(getNickName());
        System.out.println(getAdminName());
        System.out.println(getTwoSurname());
        System.out.println(getName());
    }


    //产生length长度的随机字符串
    public static String getRandomString(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*?";
        int L = str.length();
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(L);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }
    
    static String surname = "辕轩,令狐,钟离,宇文,长孙,幕容,鲜于,闾丘,司徒,司空,丌官,司寇,仉督,子车,颛孙,端木,巫马,公西,漆雕,乐正,壤驷,公良,拓拔,夹谷,宰父,谷梁,晋楚,阎法,汝鄢,涂钦,段干,百里,东郭,南门,呼延,归海,羊舌,微生,岳帅,缑亢,况后,有琴,梁丘,左丘,东门,西门,商牟,佘佴,佰赏,南官,墨哈,谯笪,年爱,阳佟,第五,言福";
    static String[] surnameArray = {};

    static {
        surnameArray = surname.split(",");
    }

    // 随机函数

    public static ThreadLocalRandom getRandom(){
        return ThreadLocalRandom.current();
    }
    /**
     * 生成网络昵称
     *
     * @return 昵称
     */
    private static String getNickName() {
        int headNameIndex = getRandom().nextInt(Constant.headerNameArray.length);
        int foodNameIndex = getRandom().nextInt(Constant.foodNameArray.length);
        return Constant.headerNameArray[headNameIndex] + Constant.foodNameArray[foodNameIndex];
    }

    /**
     * 生成带有复姓的昵称
     *
     * @return 昵称
     */
    private static String getTwoSurname() {
        int headNameIndex = getRandom().nextInt(surnameArray.length);
        int foodNameIndex = getRandom().nextInt(Constant.foodNameArray.length);
        return surnameArray[headNameIndex] + Constant.foodNameArray[foodNameIndex];
    }

    /**
     * 获取管理员名称
     *
     * @return 管理员
     */
    public static String getAdminName() {
        int headNameIndex = getRandom().nextInt(Constant.headerNameArray.length);
        return Constant.headerNameArray[headNameIndex] + "管理员";
    }

    /**
     * 获取昵称
     *
     * @return 昵称
     */
    public static String getName() {
        int i = getRandom().nextInt(2);
        return i == 0 ? getNickName() : getTwoSurname();
    }


}
